package com.jackiechan.hello.day02;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * 输入一个数学计算表达式，如:1+2
 * 然后输出计算后的结果:1+2=3
 * 这里计算表达式只计算一次即可，可以使用加减乘除任意一个,可以进行小数运算。
 * @author Xiloer
 *
 */
public class Test07 {

    private static ArrayList<String> nums = new ArrayList<>();
    private static double sum = 0;
    private static String symbol = "+";

    public static void main(String[] args) {
        System.out.println("请输入数学计算表达式：");
        String str = new Scanner(System.in).nextLine();
//        String str = "1+2*6/4+9/2*7+8-9";
        try {
            count(str);
        }catch (NumberFormatException e){
//            e.printStackTrace();
            System.out.println("请检查是否有非数字参加运算");
            return;
        }
        if (Double.isInfinite(sum)){
            System.out.println("请检查是否有除零的存在");
            return;
        }
        System.out.println(str+" = "+sum);
    }

    private static void count(String str) {
        ArrayList<String> ass = addOrSubtract(str);
        for (int i = 0; i < ass.size(); i++) {
            nums.set(i,countmd(ass.get(i)));
        }
        ArrayList<String> mds = nums;
        for (int i = 0; i < mds.size(); i++) {
            if ("+".equals(mds.get(i))){
                symbol = "+";
            }else if("-".equals(mds.get(i))){
                symbol = "-";
            }else {
                if (symbol.equals("-")){
                    sum -= Double.parseDouble(mds.get(i));
                }else {
                    sum += Double.parseDouble(mds.get(i));
                }
            }
//            nums.set(i,countas(mds.get(i)));
        }
//        nums.forEach(num -> System.out.println(num));
    }


    private static String countas(String md) {
        if (md.contains("+") || md.contains("-")){
            String[] s1 = md.split("[-]");
            for (int i = 0; i < s1.length; i++) {
                if (s1[i].contains("+")){
                    String[] s2 = s1[i].split("[+]");
                    for (int j = 0; j < s2.length; j++) {
                        sum = sum + Double.parseDouble(s2[j]);
                    }
                }
                else{
                    sum = sum - Double.parseDouble(s1[i]);
                }
            }
            return String.valueOf(sum);
        }
        return md;
    }

    /**
     * 功能：计算出每个乘法和除法
     * @param
     * @return
     */
    private static String countmd(String ass) {
        double d = 1;
        ArrayList<String> numlist = new ArrayList<>();
        if (ass.contains("*") || ass.contains("/")) {
            String[] strings = ass.split("[*]");
            for (int i = 0; i < strings.length; i=i+1) {
                numlist.add(i, strings[i]);
            }
            for (int i = 0; i < strings.length; i=i+2) {
                numlist.add(i+1, "*");
            }
            ArrayList<String> numl = new ArrayList<>();
            numl.addAll(numlist);
            for (int i = 0; i < numl.size(); i++) {
                if (numl.get(i).contains("/")) {
                    ArrayList<String> nlist = new ArrayList<>();
                    String[] strings1 = numl.get(i).split("[/]");
                    for (int j = 0; j < strings1.length ; j=j+1) {
                        nlist.add(j, strings1[j]);
                    }
                    for (int j = 0; j < strings1.length; j=j+2) {
                        nlist.add(j+1, "/");
                    }
                    numlist.remove(i);
                    numlist.addAll(i, nlist);
                }
            }
            ArrayList<String> as = numlist;
            symbol = "=";
            for (int i = 0; i < as.size(); i++) {
                if ("*".equals(as.get(i))) {
                    symbol = "*";
                } else if ("/".equals(as.get(i))) {
                    symbol = "/";
                } else {
                    if (symbol.equals("/")) {
                        d /= Double.parseDouble(as.get(i));
                    } else {
                        d *= Double.parseDouble(as.get(i));
                    }
                }
            }
        }else {
            return ass;
        }
        return String.valueOf(d);
    }

    /**
     * 功能：以”*“，”/“符号划分字符串
     * @param str：参数字符串
     * @return
     */
    private static ArrayList<String> multiplyOrDivide(String str) {
        if (!str.contains("*")){
            if (!str.contains("/")){
                nums.add(str);
            }else {
                String[] subs = str.split("[/]");
                for (int i = 0; i < subs.length; i++) {
                    multiplyOrDivide(subs[i]);
                    if (i != subs.length-1) nums.add("/");
                }
            }
        }else {
            String[] adds = str.split("[*]");
            for (int i = 0; i < adds.length; i++) {
                multiplyOrDivide(adds[i]);
                if (i != adds.length-1) nums.add("*");
            }
        }
//        System.out.println(nums.toString());
        return nums;
    }

    /**
     * 功能：以”+“，”-“符号划分字符串
     * @param str：参数字符串
     * @return
     */
    private static ArrayList<String> addOrSubtract(String str) {
        if (!str.contains("+")){
            if (!str.contains("-")){
                nums.add(str);
            }else {
                String[] subs = str.split("[-]");
                for (int i = 0; i < subs.length; i++) {
                    addOrSubtract(subs[i]);
                    if (i != subs.length-1) nums.add("-");
                }
            }
        }else {
            String[] adds = str.split("[+]");
            for (int i = 0; i < adds.length; i++) {
                addOrSubtract(adds[i]);
                if (i != adds.length-1) nums.add("+");
            }
        }
//        System.out.println(nums.toString());
        return nums;
    }

}
